<?php

namespace App\Http\Controllers\Admin;
use App\DTOS\Admin\PayOrderDTO;
use App\DTOS\NeiGouDTO;
use App\Exceptions\ServiceException;
use App\Http\Controllers\Controller;
use App\Models\Customer;
use App\Models\Game\GameGoods;
use App\Models\Game\OnLine;
use App\Models\Game\OnLineInfo;
use App\Models\Game\PayOrder;
use App\Models\Game\Server;
use App\Services\Game\DouyinService;
use App\Services\Game\NeigouService;
use App\Services\Game\PayOrderService;
use App\Services\Game\QzlService;
use App\Services\Game\UserMemberService;
use App\Services\Game\UserRoleService;
use App\Utils\InitUtils;
use App\Utils\MyLog;
use App\Utils\ResponseUtil;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;


class OnLineController extends Controller
{

    public function paginate()
    {

        $admin = Auth::guard('admin')->user();
        $permissionIds = $admin->permissions->pluck('id')->toArray();
        $groups_ids = $admin['groups_ids'];
        MyLog::info('归属分组ID'.json_encode($groups_ids));
        if($groups_ids){
            $groups_ids_array = explode(",", $groups_ids);
            $serverIds = Server::query()
                ->where('mysql_game_data_can_use', 1)
                ->where('is_merge', 0)
                ->where('server_id', '!=', 901)
                ->where('server_id', '!=', 999)
                ->whereIn('group_id', $groups_ids_array)
                ->orderBy('server_id', 'asc') // 升序排序
                ->pluck('server_id')
                ->toArray();
        }else{
            $serverIds = Server::query()
                ->where('mysql_game_data_can_use', 1)
                ->where('is_merge', 0)
                ->where('server_id', '!=', 901)
                ->where('server_id', '!=', 999)
                ->where('server_id', '!=', 897)
                ->orderBy('server_id', 'asc') // 升序排序
                ->pluck('server_id')
                ->toArray();
        }

        $data = [];
        MyLog::info('服务器列表: ' . json_encode($serverIds));

        foreach ($serverIds as $serverId) {
            $onLineModel = new OnLine();
            $onLineModel->setConnection('function_' . $serverId);

            // 尝试获取第一个在线用户记录
            $result = $onLineModel->paginate()->first();

            MyLog::info('当前组装服务器ID: ' . json_encode($serverId));

            if ($result) {
                $entry = [
                    "serverId" => $serverId,
                    "onlines" => $result->onlines // 假设 onlines 是你需要的属性
                ];
                $data[] = $entry; // 只有在找到结果时才添加到数据数组
            } else {
                MyLog::info('未找到在线数据，服务器ID: ' . json_encode($serverId));
            }
        }
        $res = [];
        $res['code'] = 0;
        $res['data'] =  $data;
        return ResponseUtil::json($res);
    }

    public function paginateOnlines()
    {
        $date = request()->get('date');
        $admin = Auth::guard('admin')->user();
        $permissionIds = $admin->permissions->pluck('id')->toArray();
        $groups_ids = $admin['groups_ids'];
        $groups_ids_array = explode(",", $groups_ids);

        if ($groups_ids) {
            $services = Server::query()
                ->whereIn('group_id', $groups_ids_array)
                ->select('server_id', 'name', 'can_push')
                ->orderByRaw('CAST(server_id AS UNSIGNED) ASC')
                ->get()
                ->pluck('server_id')
                ->toArray();
        } else {
            $services = Server::query()
                ->select('server_id')
                ->orderByRaw('CAST(server_id AS UNSIGNED) ASC')
                ->get()
                ->pluck('server_id')
                ->toArray();
        }



        $model = OnLineInfo::query();
        $xAxisData = [];
        // 生成时间轴数据
        for ($hour = 0; $hour < 24; $hour++) {
            for ($minute = 0; $minute < 60; $minute += 10) {
                $time = sprintf("%02d:%02d", $hour, $minute);
                $xAxisData[] = $time;
            }
        }

        if (!$date) {
            return ResponseUtil::error("请选择日期");
        } else {
            $model->whereRaw("SUBSTR(created_at,1,10) = ?", [$date]);
        }

        // 筛选条件
        $conditions = [];
        foreach ($xAxisData as $time) {
            $start = $time;
            $end = date('H:i', strtotime("$time +10 minutes"));
            $conditions[] = "SUBSTR(created_at,12,5) >= '$start' AND SUBSTR(created_at,12,5) < '$end'";
        }

        $model->where(function ($query) use ($conditions) {
            foreach ($conditions as $condition) {
                $query->orWhereRaw($condition);
            }
        });
        if($services){

        }
        $results = $model->whereIn('server_id',$services)->selectRaw('server_id, num, SUBSTR(created_at,12,5) AS time')->get()->toArray();

        $data = [
            'xAxisData'=>$xAxisData,
            'results'=>$results,
        ];

        return ResponseUtil::success($data);
    }

}
